<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php
    require_once("class.connectSQL.inc");
    
    //instancia da classe
    $banco = new connectSQL();
	$banco1 = new connectSQL();
	$banco2 = new connectSQL();
	$banco3 = new connectSQL();
	$banco4 = new connectSQL();
	$banco5 = new connectSQL();
	if(isset($_POST["acao"])){
		
		if($_POST["acao"]=="I"){
			
			/*
			A.codProd,A.codBarProd,A.desProd
			
			*/
			
			$dados = array(
							"codLotep"  => $_POST["codLotep"],
							"codNota"  => $_POST["codNota"],
							"dataLote"  => implode("-",array_reverse(explode("/",$_POST["dataLote"]))),
							"dataVencLote"  => implode("-",array_reverse(explode("/",$_POST["dataVencLote"]))),
							"quantLote"  => $_POST["quantLote"],
							"codProd"  => $_POST["codProd"]
							);
			
			//print_r($dados);
			
			$banco->Insert("estlote", $dados);
			//die($_POST["acao"]);
			
			$msg = "Incluido com sucesso";
		}
		if($_POST["acao"]=="A"){ 
			$sql = "UPDATE estlote set codLotep = '".$_POST["codLotep"]."',codNota = '".$_POST["codNota"]."'
			,dataLote = '".implode("-",array_reverse(explode("/",$_POST["dataLote"])))."',dataVencLote = '".implode("-",array_reverse(explode("/",$_POST["dataVencLote"])))."',quantLote = '".$_POST["quantLote"]."'
			,codProd = '".$_POST["codProd"]."'
			where codLote= ".$_POST["codLote"]." ";
			//die($sql);
			
			$banco3->Generico($sql);
			//die($_POST["acao"]);
			$msg = "Alterado com sucesso";
		}
		
		}
		if(isset($_GET["excluir"])){
				$sql = "Delete from estlote where codLote= ".$_GET["cod"]." ";
				//die($sql);
				
				$banco3->Generico($sql);
				//die($_POST["acao"]);
				$msg = "Excluido com sucesso";
				
			}
	
?>

	<head>
		<?php include "head.php"; ?>
         <script>   
    function validar(form){
    if(form.codNotaFiscal.value == ''){
        alert("O campo Nota Fiscal é obrigatório.");
        return false;
    }
	if(form.valNota.value == ''){
        alert("O campo Valor da Nota é obrigatório.");
        return false;
    }
   
    return true;
	}
  </script>   
   <script>

 function VerificaData(digData) 
	{
	var bissexto = 0;
	var data = digData; 
	var tam = data.length;
	if (tam == 10) 
	{
		var dia = data.substr(0,2)
		var mes = data.substr(3,2)
		var ano = data.substr(6,4)
		if ((ano > 1900)||(ano < 2100))
		{
			switch (mes) 
			{
				case '01':
				case '03':
				case '05':
				case '07':
				case '08':
				case '10':
				case '12':
					if  (dia <= 31) 
					{
						return true;
					}
					break
				
				case '04':		
				case '06':
				case '09':
				case '11':
					if  (dia <= 30) 
					{
						return true;
					}
					break
				case '02':
					/* Validando ano Bissexto / fevereiro / dia */ 
					if ((ano % 4 == 0) || (ano % 100 == 0) || (ano % 400 == 0)) 
					{ 
						bissexto = 1; 
					} 
					if ((bissexto == 1) && (dia <= 29)) 
					{ 
						return true;				 
					} 
					if ((bissexto != 1) && (dia <= 28)) 
					{ 
						return true; 
					}			
					break						
			}
		}
	}	
	alert("A Data "+data+" é inválida!");
	return false;
}

       </script>

	</head>
	<body>
    	<!-- Header -->
        <div id="header">
           <?php include "sair.php"; ?>
            
            <!-- Header. Main part -->
            <div id="header-main">
                <div class="container_12">
                    <div class="grid_12">
                        <div id="logo">
                            <ul id="nav">
                                <li id="current"><a href="">LOTE</a></li>
                                
                            </ul>
                        </div><!-- End. #Logo -->
                    </div><!-- End. .grid_12-->
                    <div style="clear: both;"></div>
                </div><!-- End. .container_12 -->
            </div> <!-- End #header-main -->
            <div style="clear: both;"></div>
            <!-- Sub navigation -->
          
        </div> <!-- End #header -->
        
		<div class="container_12" align="center" >
        

            <div class="module">
                     <h2><span>Cadastro de Lote</span></h2>
                        
                     <div class="module-body">
                        <form action="cadlote.php?codNota=<?php echo $_GET["codNota"]; ?>" onsubmit="return validar(this);" id="form" method="post">
                        <p align="left">
                            <?php
					 		$cod = $banco1->GerarCod("codLote","estlote");
							if($cod < 1){
								$cod = 1;
								}
							
							
							?> 
                            <input type="hidden" name="codNota" id="codNota" value="<?php  echo $_GET["codNota"]; ?>" />
                            
                                <label>Código Lote</label>
                                <label><?php  if(isset($_GET["altera"])){echo $_GET["cod"];}else{echo $cod;} ?></label>
                                <input type="hidden" name="codLote" id="codLote" value="<?php  if(isset($_GET["altera"])){echo $_GET["cod"];}else{echo $cod;} ?>" />
                                
                            </p>
                            <?php
							 	
							
					 	 if(isset($_GET["altera"])){
							 $resultado = $banco2->Query("SELECT * FROM estlote where codLote= ".$_GET["cod"]);
							 foreach ($resultado as $resut){
								$desc =  $resut['codNota'];
								$desc1 =  $resut['codProd'];
								$desc2 =  $resut['codLotep'];
								$desc4 =  $resut['dataLote'];
								$desc5 =  $resut['dataVencLote'];
								$desc6 =  $resut['quantLote'];
							 }
						 }else{
							 	$desc = "";
								$desc1 = "";
								$desc2 = "";
								$desc4 = "";
								$desc5 = "";
								$desc6 = "";
						  } 
						 
				?>
                            <p align="left">
                                <label>Código da Nota</label>
                                <label><?php 
								
								 $resultado = $banco4->Query("SELECT A.codNota, A.codFornec, A.CodNotaFiscal, A.valNota, (
													SELECT COUNT( B.codNota ) 
													FROM estlote AS B
													WHERE B.codNota = A.codNota
													) AS lote, (
													SELECT desForn 
													FROM estfornec AS B
													WHERE B.codFornec = A.codFornec
													) AS fornec
													FROM estnota AS A where codNota =' ".$_GET["codNota"]."'");
								 
								
							 foreach ($resultado as $resut){
								$desc7 =  $resut['CodNotaFiscal'];
							 }
							
								echo $desc7; ?></label>
                                
                            </p>
                            <p align="left">
                                <label>Código do Lote</label>
                                <input type="text" class="input-medium" id="codLotep" name="codLotep" value="<?php echo $desc2; ?>" />
                            </p>
                            
                                    <p align="left">
                                <label>Data do Lote</label>
                                <input type="text" class="input-medium" id="dataLote"  onBlur="VerificaData(this.value);" name="dataLote" value="<?php echo implode("/",array_reverse(explode("-", $desc4))); ?>" />
                              </p>
                              <p align="left">
                                <label>Data de Vencimento do Lote</label>
                                <input type="text" class="input-medium" id="dataVencLote"  onBlur="VerificaData(this.value);" name="dataVencLote" value="<?php echo implode("/",array_reverse(explode("-", $desc5))); ?>" />
                              </p>
                            <p align="left">	
                                <label>Produto</label>
                                <select class="input-medium" name="codProd" id="codProd">
                                <option value="">Selecione um produto</option>
                                 <?php
										 $resultado = $banco->Query("SELECT * FROM estprod");
										foreach ($resultado as $resut){
											$codProd = $resut['codProd'];
											if($desc1 == $codProd ){
												echo "<option value='".$codProd."' selected>".$resut['desProd']."</option>";
											}else{
												echo "<option value=".$codProd." >".$resut['desProd']."</option>";
											}
												
											
										
										}
		?>
                               </select>
                            </p>
                            <p align="left">
                                <label>Quantidade</label>
                                <input type="text" class="input-medium" id="quantLote" name="quantLote" value="<?php echo $desc6; ?>" />
                             </p>
                 
                            
                            
                           <input type="hidden" name="acao" value="<?php if(isset($_GET["altera"])){echo "A";}else{echo "I";} ?>" />
                            <fieldset>
                                <input class="submit-green" type="submit" value="Salvar" /> 
                                <input class="submit-gray" type="submit" value="Cancelar" />
                            </fieldset>
                        </form>
                     </div> <!-- End .module-body -->
					 
                </div>  <!-- End .module -->
				<?php 
				if(isset($msg)){
				echo "<span class='notification n-success'>".$msg."</span>";
				}
				?>
				
                <!-- Example table -->
                <div class="module">
                	<h2>Lotes</h2>
                    
                    <div class="module-table-body">
                    	<form action="">
                        <table width="89%" class="tablesorter" id="myTable">
                        	
                                <tr>
                                   <th width="4%" style="width:5%">#</th>
                                   <th width="9%" style="width:10%">Código Lote</th>
                                   <th width="9%" style="width:10%">Data do Lote</th>
                                   <th width="9%" style="width:10%">Data do Vencimento</th>
                                   <th width="36%" style="width:30%">Produto</th>
                                   <th width="10%" style="width:10%">Quantidade</th>
                                   <th width="23%" style="width:35%"></th>
                                </tr>
                         <?php
						 
						

				$resultado = $banco5->Query("SELECT A.quantLote, A.dataVencLote, A.dataLote, A.codLotep, A.codNota, A.codProd, A.codLote, (
								SELECT B.desProd
								FROM estprod AS B
								WHERE B.codProd = A.codProd
								) AS produto
								FROM estlote AS A where codNota =  ".$_GET["codNota"]);
                            
    
        foreach ($resultado as $resut){
        echo "<tr>";
		echo "<td>". $resut['codLote']."</td>";
		echo "<td>". $resut['codLotep']."</td>";
		echo "<td>".implode("/",array_reverse(explode("-",$resut['dataLote'])))."</td>";
		echo "<td>".implode("/",array_reverse(explode("-",$resut['dataVencLote'])))."</td>";
		echo "<td>". $resut['produto']."</td>";
		echo "<td>". $resut['quantLote']."</td>";
		?>
        <td>
        
        <a href="cadlote.php?altera=1&cod=<?php echo $resut['codLote']; ?>&codNota=<?php echo $_GET["codNota"]; ?>"><img src="pencil.gif"  width="30" height="30" alt="edit" /></a>
        
        <a href="cadlote.php?excluir=1&cod=<?php echo $resut['codLote']; ?>&codNota=<?php echo $_GET["codNota"]; ?>"><img src="bin.gif"  width="30" height="30" alt="delete" /></a>
        <?php echo "</tr>";
		}
   		                  ?>  
                                
                     
                      </tbody>    
                           
                        </table>
                        </form>
                     
                     
                 </div>    
                     
                     
            <div style="clear:both;"></div>
            
            <div style="clear:both;"></div>
        </div> <!-- End .container_12 -->
		
           
       <?php include "footer.php"; ?>
	</body>
</html>